package com.aaa.service.impl;

import com.aaa.mapper.*;
import com.aaa.service.RoomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RoomServiceImpl implements RoomService {
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private OrderdetailsMapper orderdetailsMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private BillMapper billMapper;
    @Resource
    private ChangeMapper changeMapper;
    @Override
    public List<Map<String, Object>> findroom(String startdate, String enddate, Integer rtid) {
        List<Map<String, Object>> list1=roomMapper.findroom(rtid,"可入住");
        List<Map<String, Object>> list2=orderdetailsMapper.findroom(startdate,enddate,rtid,"已入住");
        if(list2!=null&&list2.size()>0){
            for (int i = 0; i < list1.size(); i++) {
                for (int j = 0; j < list2.size(); j++) {
                    if(list1.get(i).get("id")==list2.get(j).get("id")){
                        list1.remove(i);
                    }
                }
            }
        }
        return list1;
    }

    @Override
    public PageInfo<Map<String, Object>> roomload(Integer rtid, String state, Integer pageNum, Integer pageSize) throws ParseException {
        List<Map<String, Object>> list2=orderdetailsMapper.findroom(null,null,null,null);
        List<Map<String, Object>> list3=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        if(list2!=null&&list2.size()>0){
            System.out.println(list2);
            for (int i = 0; i < list2.size(); i++) {
                if(list2.get(i).get("state").equals("已入住")||list2.get(i).get("state").equals("待退房")){
                    Date enddate=null;
                    if(list2.get(i).get("state").equals("待退房")){
                        enddate=  sdf.parse(list2.get(i).get("enddate").toString());
                        System.out.println(now.getTime()>=enddate.getTime());
                        if(now.getTime()>=enddate.getTime()){
                            Integer id= (Integer) list2.get(i).get("id");
                            //取消超时未入住订单
                            roomMapper.OverTimeState(id,"待退房");
                        }
                    }
                    list3.add(list2.get(i));
                }
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> list1=roomMapper.roomload(rtid,state);
        PageInfo<Map<String,Object>> pageInfo=new PageInfo<>(list1);
        return pageInfo;
    }

    @Override
    public Object checkout(Integer rid) throws ParseException {
        List<Map<String ,Object>> list1=orderdetailsMapper.findodid(rid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        Date now = new Date();
        //结束时间
        String end= list1.get(0).get("enddate").toString().substring(0,19);
        Date endDate = sdf.parse(end);
        double betweenDate = Math.ceil(Double.parseDouble(String.valueOf(( now.getTime()-endDate.getTime())))/(1000L*3600L*24L));
        betweenDate=betweenDate-1;
        //打印控制台相差的天数
        System.out.println("betweenDate"+betweenDate);
        System.out.println(list1.get(0).get("money"));
        Integer m= Integer.parseInt(list1.get(0).get("money").toString());
        int money= (int) (m*betweenDate);
//        orderdetailsMapper.checkoutstate((Integer) list1.get(0).get("id"));
//        List<Map<String ,Object>> list2=orderdetailsMapper.findStateByOid((Integer) list1.get(0).get("orderid"));
//        boolean tag=false;
//        if(list2.size()>0&&list2!=null){
//            for (int i = 0; i < list2.size(); i++) {
//                if(list2.get(i).get("state").equals("已完成")){
//                    tag=true;
//                }else {
//                    tag=false;
//                }
//            }
//        }
//        if(tag){
//            orderMapper.checkoutState((Integer) list1.get(0).get("orderid"));
//        }
//        roomMapper.checkout(rid);
        Map<String ,Object> map=new HashMap<>();
        map.put("money",money);
        map.put("orderid",(Integer) list1.get(0).get("orderid"));
        return map;
    }

    @Override
    public List<Map<String, Object>> findchangeroom(Integer rid, Integer odid, String enddate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        Date now = new Date();
        String startdate=sdf.format(now);
        List<Map<String, Object>> list1=roomMapper.findallroom();
        String state="已完成";
        System.out.println(startdate);
        System.out.println(enddate);
        List<Map<String, Object>> list2=orderdetailsMapper.findinroom(startdate,enddate,state);
        System.out.println(list2);
        if(list1!=null&&list1.size()>0){
            if(list2!=null&&list2.size()>0){
                for (int i = 0; i < list1.size(); i++) {
                    for (int j = 0; j < list2.size(); j++) {
                        if(list2.get(j)!=null){
                            if(list1.get(i).get("id").equals(list2.get(j).get("id"))){
                                list1.remove(i);
                            }
                        }
                    }
                }
            }
        }
        return list1;
    }

    @Override
    public Object changeroom(Integer newrid, Integer odid, double money, Integer oldrid, Integer staff) throws ParseException {
        String operate="换房";
        billMapper.addbill(operate,money,odid,"", staff);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String changedate=sdf.format(now);
        Integer rtid=Integer.parseInt(roomMapper.findrt(newrid).get("tid").toString());
        orderdetailsMapper.changeroom(odid,newrid,rtid);
        roomMapper.changenewstate(newrid);
        roomMapper.changeoldstate(oldrid);
        Map<String,Object> map=orderdetailsMapper.findorder(odid);
        Integer oid=Integer.parseInt(map.get("orderid").toString());
        orderMapper.changemoney(money,oid);
        return changeMapper.changeroom(odid,changedate,oldrid,newrid,staff);
    }

    @Override
    public Integer addroom(String rname, Integer tid, String state) {
        return roomMapper.addroom(rname,tid,state);
    }

    @Override
    public List<Map<String, Object>> checkroom(String rname) {
        return roomMapper.checkroom(rname);
    }

    @Override
    public List<Map<String, Object>> findReadyRoom(String enddate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //开始时间
        Date now = new Date();
        //各房间类型总数
        List<Map<String, Object>> list1=roomMapper.findroomnum();
        //已入住和预定的各房间类型数量
        System.out.println(sdf.format(now));
        List<Map<String, Object>> list2=roomMapper.findOrderedRoomnum(sdf.format(now),enddate,"已完成");
        System.out.println(list2);
        Map<String,Object> map=new HashMap<>();
        if(list1.size()>0&&list1!=null){
            if(list2.size()>0&&list2!=null){
                for (int i = 0; i < list1.size(); i++) {
                    for (int j = 0; j < list2.size(); j++) {
                        if(list1.get(i).get("tid").equals(list2.get(j).get("tid"))){
                            Integer num=Integer.parseInt(list1.get(i).get("num").toString())-Integer.parseInt(list2.get(j).get("num").toString());
                            System.out.println(num);
                            if(num==0){
                                list1.remove(i);
                            }
                        }
                    }
                }
            }
        }
        System.out.println(list1);
        return roomMapper.findReadyRoom(list1);
    }


    @Override
    public Object changeRoomMoney(Integer oldrid, Integer newrid, String enddate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("---------");
        System.out.println(enddate);
        //开始时间
        Date now = new Date();
        //结束时间
        Date endDate = sdf.parse(enddate);
        System.out.println(endDate);
        double betweenDate = Double.parseDouble(String.valueOf(( endDate.getTime()-now.getTime())))/(1000L*3600L*24L);
        System.out.println(betweenDate);
        Map<String ,Object> map1=roomMapper.findnewmoney(newrid);
        Map<String ,Object> map2=roomMapper.findoldmoney(oldrid);
        System.out.println(map1.get("money")+"   "+map2.get("money"));
        System.out.println(Integer.parseInt(map1.get("money").toString())-Integer.parseInt(map2.get("money").toString()));
        double money=(Integer.parseInt(map1.get("money").toString())-Integer.parseInt(map2.get("money").toString()))*betweenDate;
        System.out.println(money);
        return String.format("%.0f", money);



    }
}
